package server.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import server.vo.TbExpenseVO;

public class TbExpenseDAO {
	
	public TbExpenseVO inqrAcntBalance(Connection conn, TbExpenseVO tbExpenseVO) throws Exception {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {		
			StringBuffer sql = new StringBuffer();
			sql.append("\n SELECT a.acnt_balance                                 " );
			sql.append("\n   FROM dbo.TB_EXPENSE_BUDGET a                        " );
			sql.append("\n  WHERE a.pjt_code = ?                                 " );
			sql.append("\n    AND a.acnt_code = ?                                " );
			
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setString(1, tbExpenseVO.getPjt_code());
			pstmt.setString(2, tbExpenseVO.getAcnt_code());
			rs = pstmt.executeQuery();

			TbExpenseVO ExpenseVO = new TbExpenseVO();
			if (rs.next()){
				ExpenseVO.setAcnt_balance(rs.getInt("acnt_balance"));	
			}
			
			return ExpenseVO;

		} catch (Exception e) {
			throw e;
		} finally {
			if (rs != null)
				rs.close();

			if (pstmt != null)
				pstmt.close();
		}
	}
	
	public boolean updateAcntBalance(Connection conn, TbExpenseVO tbExpenseVO) throws Exception {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {		
			StringBuffer sql = new StringBuffer();
			sql.append("\n UPDATE dbo.TB_EXPENSE_BUDGET                         " );
			sql.append("\n SET acnt_balance = ?                                 " );
			sql.append("\n WHERE pjt_code = ?                                   " );
			sql.append("\n   AND acnt_code = ?                                  " );
	
			pstmt = conn.prepareStatement(sql.toString());
			
			pstmt.setInt(1, tbExpenseVO.getAcnt_balance());
			pstmt.setString(2, tbExpenseVO.getPjt_code());
			pstmt.setString(3, tbExpenseVO.getAcnt_code());

			return pstmt.executeUpdate() > 0 ? true : false;

		} catch (Exception e) {
			return false;
		} finally {
			if (rs != null)
				rs.close();

			if (pstmt != null)
				pstmt.close();
		}
	}
}
